package com.socialrss.utils;

import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingFragmentActivity;
import com.socialrss.R;
import com.socialrss.constants.SocialNetworkNames;
import com.socialrss.constants.SocialNetworkNames.SocialNetKeys;
import com.socialrss.utils.twitter.TwitterUtil;
import com.socialrss.viewlisteners.ApplicationSettings;
import com.socialrss.viewlisteners.Credits;
import com.socialrss.viewlisteners.OuterCategoryViewController;
import com.socialrss.viewlisteners.SlidingFragment;
import com.socialrss.viewlisteners.TwitterLoginActivity;
import com.socialrss.viewlisteners.ManageSocialNetworks;
import com.socialrss.viewlisteners.ManageConnections;
import com.socialrss.utils.facebook.FacebookUtil;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;

public class Util {

	public static void goToGitHub(Context context) {
		Uri uriUrl = Uri.parse("http://github.com/jfeinstein10/slidingmenu");
		Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
		context.startActivity(launchBrowser);
	}

	public static void setSlidingMenu(SlidingFragmentActivity activity) {
		// set the Behind View
		activity.setBehindContentView(R.layout.menu_frame);
		activity.getSupportFragmentManager().beginTransaction()
				.replace(R.id.menu_frame, new SlidingFragment(activity))
				.commit();
		// customize the SlidingMenu
		SlidingMenu sm = activity.getSlidingMenu();
		sm.setShadowWidthRes(R.dimen.shadow_width);
		sm.setShadowDrawable(R.drawable.shadow);
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setFadeDegree(0.35f);
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

	}

	// change the screen, transit to another activity
	public static void changeScreen(Activity activity, int groupPosition,
			int childPosition) {
		Intent intent = null;
		if (groupPosition == 0) {

		} else {
			switch (childPosition) {
			case 0:
				intent = new Intent(activity, OuterCategoryViewController.class);
				break;
			case 1:
				intent = new Intent(activity,ManageConnections.class);
				break;
			case 2:
				intent = new Intent(activity, ManageSocialNetworks.class);
				break;
			case 3:
				intent = new Intent(activity, ApplicationSettings.class);
				break;
			case 4:
				intent = new Intent(activity, Credits.class);
				break;
			default:
				break;
			}
		}
		if(intent != null) {
			activity.startActivity(intent);
		}
	}
	
	/**
	 * Checking for all possible internet providers
	 * **/
	public static boolean isConnectingToInternet(Context context) {
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity != null) {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null)
				for (int i = 0; i < info.length; i++)
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}

		}
		return false;
	}
	
	/**
	 * Function to display simple Alert Dialog
	 * 
	 * @param context
	 *            - application context
	 * @param title
	 *            - alert dialog title
	 * @param message
	 *            - alert message
	 * @param status
	 *            - success/failure (used to set icon) - pass null if you don't
	 *            want icon
	 * */
	@SuppressWarnings("deprecation")
	public static void showAlertDialog(Context context, String title,
			String message, Boolean status) {
		AlertDialog alertDialog = new AlertDialog.Builder(context).create();

		// Setting Dialog Title
		alertDialog.setTitle(title);

		// Setting Dialog Message
		alertDialog.setMessage(message);

		if (status != null)
			// Setting alert dialog icon
			alertDialog
					.setIcon((status) ? R.drawable.success : R.drawable.fail);

		// Setting OK Button
		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
			}
		});

		// Showing Alert Message
		alertDialog.show();
	}

	public static boolean isConnected(String socialNetwork, Context con) {
		if(socialNetwork == SocialNetworkNames.INSTAGRAM) {
			return SocialRssModel.Instance(con).getAccessTokenOfSocialNet(SocialNetKeys.ins.toString()).isValid();
		}
		else if(socialNetwork == SocialNetworkNames.TWITTER) {
			return TwitterUtil.isTwitterLoggedInAlready();
		}
		else if(socialNetwork == SocialNetworkNames.FACEBOOK) {
			return FacebookUtil.Instance(null).isConncted();
		}
		else if(socialNetwork == SocialNetworkNames.GOOGLE) {
			return false;
		}
		return false;
	}
}
